/*
 *  Copyright 2009 Brian Mackay brianwmackay@msn.com
 *   All Rights Reserved. See end of file
 *   Some portions of this class where taken from hadoop JUNIT tests
 */

package com.bmackay.vonsteuben.job.display;

import org.apache.hadoop.mapred.jobcontrol.JobControl;

public class JobThread {

     public void showProgress(JobControl jobControl) {

        Thread theController = new Thread(jobControl);
        theController.start();
        int waiting = 0;
        int ready = 0;
        int running = 0;
        int successful = 0;
        int failed = 0;
        boolean showProgress = true;

        while (!jobControl.allFinished()) {

            if (waiting != jobControl.getWaitingJobs().size()) {
                waiting = jobControl.getWaitingJobs().size();
                showProgress = true;
            }
            if (ready != jobControl.getReadyJobs().size()) {
                ready = jobControl.getReadyJobs().size();
                showProgress = true;
            }
            if (running != jobControl.getRunningJobs().size()) {
                running = jobControl.getRunningJobs().size();
                showProgress = true;
            }
            if (failed != jobControl.getFailedJobs().size()) {
                failed = jobControl.getFailedJobs().size();
                showProgress = true;
            }
            if (successful != jobControl.getSuccessfulJobs().size()) {
                successful = jobControl.getSuccessfulJobs().size();
                showProgress = true;
            }

            if (showProgress == true) {
                showProgress = false;
                System.out.println("Jobs in waiting state: " + waiting);
                System.out.println("Jobs in ready state: " + ready);
                System.out.println("Jobs in running state: " + running);
                System.out.println("Jobs in success state: " + successful);
                System.out.println("Jobs in failed state: " + failed + "\n \n");

            }

            try {
                Thread.sleep(3000);
            } catch (Exception e) {
                System.out.println("!!!!!!!!!!!Exception:" + e);
            }
        }


        if (jobControl.getFailedJobs().size() > 0) {
            for (int i = 0; i <
                    jobControl.getFailedJobs().size(); i++) {

                System.out.println("Failed Job: " + jobControl.getFailedJobs().get(i).getMessage());
                System.out.println("\n");
            }
        }
    }
}

/*
 * Copyright 2009 Brian Mackay brianwmackay@msn.com
 * All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
